using System;
using CodeAngel.Domain.Framework;

namespace CodeAngel.Domain
{
	public partial class PropertyDefCollection
    {
        #region HasEntityProperties
        
        /// <summary>
        /// To identify if any property definition within this collection is an entity.
        /// </summary>
        public bool HasEntityProperties
        {
            get
            {
                foreach (var ThisPD in this)
                    if (ThisPD.IsEntityType)
                        return true;
                return false;
            }
        }

        #endregion

        #region HasCollectionProperties
        
        /// <summary>
        /// To identify if any property definition within this collection is an entity collection.
        /// </summary>
        public bool HasCollectionProperties
        {
            get
            {
                foreach (var ThisPD in this) if (ThisPD.IsCollectionType)
                        return true;
                return false;
            }
        }

        #endregion

        #region Subsets

        /// <summary>
        /// To identify if any property definition within this collection is an entity or collection.
        /// </summary>
        public bool HasEntityOrCollectionProperties
        {
            get { return this.HasEntityProperties || this.HasCollectionProperties; }
        }

        /// <summary>
        /// To identify the property definitions within this collection that are scalar type 
        /// properties (not Entity or collection properties).
        /// </summary>
        public PropertyDefCollection ScalarProperties
        {
            get
            {
                var Properties = new PropertyDefCollection(this.Aggregate, this.ParentEntity);
                foreach (var ThisPD in this)
                    if (ThisPD.IsScalarType)
                        Properties.Add(ThisPD);
                return Properties; //.GetSortedView(SortOnEnum.Name);
            }
        }

        /// <summary>
        /// To identify the property definitions within this collection that are entity definitions.
        /// </summary>
        public PropertyDefCollection EntityProperties
        {
            get
            {
                var Properties = new PropertyDefCollection(this.Aggregate, this.ParentEntity);
                foreach (var ThisPD in this)
                    if (ThisPD.IsEntityType) Properties.Add(ThisPD);
                return Properties; //.GetSortedView(SortOnEnum.Name);
            }
        }

        /// <summary>
        /// To identify the property definitions within this collection that are scalar type or 
        /// entity defintion properties.
        /// </summary>
        public PropertyDefCollection ScalarOrEntityProperties
        {
            get
            {
                var Properties = new PropertyDefCollection(this.Aggregate, this.ParentEntity);
                foreach (var ThisPD in this)
                    //if (ThisPD.IsScalarType || ThisPD.IsEntityType)
                    if (!ThisPD.IsCollectionType)
                        Properties.Add(ThisPD);
                return Properties; //.GetSortedView(SortOnEnum.Name);
            }
        }

        /// <summary>
        /// To identify the property definitions within this collection that are entity definition 
        /// Collections.
        /// </summary>
        public PropertyDefCollection CollectionProperties
        {
            get
            {
                var Properties = new PropertyDefCollection(this.Aggregate, this.ParentEntity);
                foreach (var ThisPD in this)
                    if (ThisPD.IsCollectionType)
                        Properties.Add(ThisPD);
                return Properties; //.GetSortedView(SortOnEnum.Name);
            }
        }

        /// <summary>
        /// To identify the property definitions within this collection that are entity defintions 
        /// or collections.
        /// </summary>
        public PropertyDefCollection EntityOrCollectionProperties
        {
            get
            {
                var Properties = new PropertyDefCollection(this.Aggregate, this.ParentEntity);
                foreach (var ThisPD in this)
                    if (ThisPD.IsEntityType || ThisPD.IsCollectionType)
                        Properties.Add(ThisPD);
                return Properties; //.GetSortedView(SortOnEnum.Name);
            }
        }

        #endregion

        #region Find
        
        /// <summary>
        /// Finds the first PropertyDef object in this collection with the identified name.
        /// Returns null if not found.
        /// </summary>
        public PropertyDef Find(string name)
        {
            foreach (var ThisPD in this)
                if (ThisPD.Name == name) return ThisPD;
            return null;
        }

        #endregion

        #region FindPrimaryKey
        
        /// <summary>
        /// Returns the PropertyDef from this collection flagged as the Primary Key. Returns
        /// null if not found.
        /// </summary>
        public PropertyDef FindPrimaryKey()
        {
            foreach (var ThisPD in this)
                if (ThisPD.IsPrimaryKey) return ThisPD;
            return null;
        }

        #endregion

        #region Contains
        
        /// <summary>
        /// To identify a PropertyDef with the same name already exists in this collection. 
        /// No children will be searched.
        /// </summary>
        public bool Contains(string name)
        {
            foreach (var ThisPD in this)
                if (ThisPD.Name == name) return true;
            return false;
        }

        #endregion
    }
}

